package com.zyd.shiro.business.service;

import com.zyd.shiro.business.vo.TalentShareVO;
import com.zyd.shiro.persistence.beans.Talentshare;
import com.zyd.shiro.persistence.dao.MallTalentshareDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务层
 *
 * @author RookieWZW
 */
@Service
public class MallTalentshareService {

    @Autowired
    private MallTalentshareDao talentshareDao;


    public List<Talentshare> findAll() {

        return talentshareDao.findAll();
    }

    /**
     * 根据招聘/简历    信息状态查询
     *
     * @param state
     * @param mold
     * @return
     */
    public List<Talentshare> findALLByStateAndMold(Integer state, Integer mold) {
        return talentshareDao.getAllByStateAndMold(state, mold);
    }

    /**
     * 根据全职/兼职    招聘/简历  信息状态查询
     *
     * @param type
     * @param mold
     * @param state
     * @return
     */
    public List<Talentshare> findAllByTypeAndAndMoldAAndState(Integer type, Integer mold, Integer state) {
        return talentshareDao.getAllByTypeAndMoldAndState(type, mold, state);
    }

    public List<Talentshare> findALLByUserID(Integer user_id) {
        return talentshareDao.getAllByUser_id(user_id);
    }

    public List<Talentshare> findAllByState(Integer state) {
        return talentshareDao.getAllByState(state);
    }

    /**
     * 修改审核状态
     *
     * @param state
     * @param id
     */

    public void updateState(Integer state, Integer id) {
        talentshareDao.updateState(state, id);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Talentshare> findPage(int page, int size) {
        PageRequest pageRequest = new PageRequest(page - 1, size);
        return talentshareDao.findAll(pageRequest);
    }

    private Specification<Talentshare> where(Map searchMap) {

        return new Specification<Talentshare>() {

            @Override
            public Predicate toPredicate(Root<Talentshare> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 标题
                if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))) {
                    predicateList.add(cb.like(root.get("title").as(String.class), "%" + (String) searchMap.get("title") + "%"));
                }
                // 职位
                if (searchMap.get("position") != null && !"".equals(searchMap.get("position"))) {
                    predicateList.add(cb.like(root.get("position").as(String.class), "%" + (String) searchMap.get("position") + "%"));
                }
                // 求职要求描述
                if (searchMap.get("request") != null && !"".equals(searchMap.get("request"))) {
                    predicateList.add(cb.like(root.get("request").as(String.class), "%" + (String) searchMap.get("request") + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

    public Page<Talentshare> findSearch(Map whereMap, int page, int size) {
        Specification<Talentshare> specification = where(whereMap);
        PageRequest pageRequest = new PageRequest(page - 1, size);
        return talentshareDao.findAll(specification, pageRequest);
    }

    public Talentshare findOne(Integer id) {
        return talentshareDao.findOne(id);
    }

    public void add(Talentshare talentshare) {

        talentshareDao.save(talentshare);
    }

    public void update(Talentshare talentshare) {
        talentshareDao.save(talentshare);
    }

    public void delete(Integer id) {
        talentshareDao.delete(id);
    }

    public void deleteList(Integer[] ids) {
        for (Integer id : ids) {
            talentshareDao.delete(id);
        }
    }

    public Page<Talentshare> findPageByVO(TalentShareVO data) {
        Map map = new HashMap();
        if (!StringUtils.isEmpty(data.getTitle()))
            map.put("title", data.getTitle());
        Specification<Talentshare> specification = where(map);
        PageRequest pageRequest = new PageRequest(data.getPageNumber() - 1, data.getPageSize());
        Page<Talentshare> page = talentshareDao.findAll(specification, pageRequest);
        return page;
    }
}
